package com.yiren.dbaa.module.practice_set.site.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.site.bean.CityBean;
import com.yiren.dbaa.module.practice_set.site.bean.ProvinceBean;
import com.yiren.dbaa.module.practice_set.site.contract.AddSiteRuleContract;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.TestBaseBean;
import com.yiren.dbaa.net.interf.StringCallback;
import java.util.List;

/**
 * @author lwb
 * @date : 2022/9/23 10:00
 * @description:  新建规则M
 */
public class AddSiteRuleModel implements AddSiteRuleContract.Model {

    private Activity activity;
    private Gson  gson;

    public AddSiteRuleModel(Activity activity) {
        this.activity = activity;
        gson = new Gson();
    }

    @Override
    public void getShareSiteRule(String city, String user, CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.loadExamRuleByPid("?pid="+city+"&user="+user), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean<List<SiteEntity>> testBaseBean = gson.fromJson(result,new TypeToken<TestBaseBean<List<SiteEntity>>>() {}.getType());
                if (testBaseBean.isSuccess()){
                    if (testBaseBean.getData()!=null){
                        callBack.success(testBaseBean.getData());
                    }else {
                        callBack.fail("当前城市查询结果为空!");
                    }
                }else {
                    callBack.fail("查询失败!");
                }
            }
            @Override
            public void getFail() {
                callBack.fail("数据请求失败!");
            }
        });
    }


    @Override
    public void getProvince(CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.loadRegionByPid("?pid=0"), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean<List<ProvinceBean>>   testBaseBean = gson.fromJson(result,new TypeToken<TestBaseBean<List<ProvinceBean>>>() {}.getType());
                if (testBaseBean.isSuccess()){
                    callBack.success(testBaseBean.getData());
                }
            }

            @Override
            public void getFail() {
                    callBack.fail("数据请求失败!");
            }
        });
    }

    @Override
    public void getCity(String pid, CommonCallBack callBack) {
        OkHttpUtils.getInstance().getStringByAsync(activity, AddressConstant.loadRegionByPid("?pid="+pid), new StringCallback() {
            @Override
            public void getString(String result) {
                TestBaseBean<List<CityBean>>   testBaseBean = gson.fromJson(result,new TypeToken<TestBaseBean<List<CityBean>>>() {}.getType());
                if (testBaseBean.isSuccess()){
                    callBack.success(testBaseBean.getData());
                }
            }

            @Override
            public void getFail() {
                callBack.fail("数据请求失败!");
            }
        });
    }
}
